package com.woniu.consumer;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.woniu.utils.ConstantUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
@Slf4j
public class SurgeryConsumer {
    @RabbitListener(queues = {ConstantUtil.QUEUE_SURGERY_REMINDER})
    public void consumeSurgeryReminder(String msg, Channel channel, Message message) {
        log.info("=== 收到手术延迟提醒消息 ===");

        try {
            Map<String, Object> messageMap = JSON.parseObject(msg, Map.class);
            String patientName = (String) messageMap.get("patientName");
            String surgeryName = (String) messageMap.get("surgeryName");
            String doctorName = (String) messageMap.get("doctorName");
            String reminderMessage = (String) messageMap.get("message");
            log.info("🔔 手术提醒通知护士：{}", reminderMessage);
            log.info("📋 详情 - 患者：{}，手术：{}，医生：{}",
                    patientName, surgeryName, doctorName);

            // 1. 发送站内信给所有护士
            // 2. 推送系统通知
            // 3. 记录到操作日志

            // 确认消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
//            log.info("手术提醒处理完成");

        } catch (Exception e) {
            log.error("处理手术提醒失败：", e);
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
            } catch (IOException ex) {
                log.error("拒绝消息失败：", ex);
            }
        }
    }
}
